package com.hifar.sso.model;

import java.io.Serializable;
import java.util.Date;

/**
 * SSO Token模型
 * 
 * @author system
 * @since 1.0.0
 */
public class SSOToken implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * 访问令牌
     */
    private String accessToken;
    
    /**
     * 刷新令牌
     */
    private String refreshToken;
    
    /**
     * 令牌类型 (Bearer)
     */
    private String tokenType = "Bearer";
    
    /**
     * 过期时间 (秒)
     */
    private Long expiresIn;
    
    /**
     * 令牌创建时间
     */
    private Date createTime;
    
    /**
     * 令牌过期时间
     */
    private Date expireTime;
    
    /**
     * 用户ID
     */
    private String userId;
    
    /**
     * 客户端ID
     */
    private String clientId;
    
    /**
     * 作用域
     */
    private String scope;
    
    /**
     * 是否已使用
     */
    private Boolean used = false;
    
    public SSOToken() {
    }
    
    public SSOToken(String accessToken, String refreshToken, Long expiresIn) {
        this.accessToken = accessToken;
        this.refreshToken = refreshToken;
        this.expiresIn = expiresIn;
        this.createTime = new Date();
        this.expireTime = new Date(createTime.getTime() + expiresIn * 1000);
    }
    
    /**
     * 检查令牌是否过期
     */
    public boolean isExpired() {
        return new Date().after(expireTime);
    }
    
    /**
     * 检查令牌是否有效
     */
    public boolean isValid() {
        return !isExpired() && !used;
    }
    
    // Getters and Setters
    public String getAccessToken() {
        return accessToken;
    }
    
    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }
    
    public String getRefreshToken() {
        return refreshToken;
    }
    
    public void setRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
    }
    
    public String getTokenType() {
        return tokenType;
    }
    
    public void setTokenType(String tokenType) {
        this.tokenType = tokenType;
    }
    
    public Long getExpiresIn() {
        return expiresIn;
    }
    
    public void setExpiresIn(Long expiresIn) {
        this.expiresIn = expiresIn;
    }
    
    public Date getCreateTime() {
        return createTime;
    }
    
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    
    public Date getExpireTime() {
        return expireTime;
    }
    
    public void setExpireTime(Date expireTime) {
        this.expireTime = expireTime;
    }
    
    public String getUserId() {
        return userId;
    }
    
    public void setUserId(String userId) {
        this.userId = userId;
    }
    
    public String getClientId() {
        return clientId;
    }
    
    public void setClientId(String clientId) {
        this.clientId = clientId;
    }
    
    public String getScope() {
        return scope;
    }
    
    public void setScope(String scope) {
        this.scope = scope;
    }
    
    public Boolean getUsed() {
        return used;
    }
    
    public void setUsed(Boolean used) {
        this.used = used;
    }
    
    @Override
    public String toString() {
        return "SSOToken{" +
                "accessToken='" + accessToken + '\'' +
                ", refreshToken='" + refreshToken + '\'' +
                ", tokenType='" + tokenType + '\'' +
                ", expiresIn=" + expiresIn +
                ", createTime=" + createTime +
                ", expireTime=" + expireTime +
                ", userId='" + userId + '\'' +
                ", clientId='" + clientId + '\'' +
                ", scope='" + scope + '\'' +
                ", used=" + used +
                '}';
    }
}
